﻿#include "positionwid.h"
#include "componentmanager.h"
#include <QMessageBox>

PositionWid::PositionWid(QWidget *parent, bool show)
	: QWidget(parent)
{
	ui.setupUi(this);
	if (show) {
		ui.pushButton->show();
	} else {
		ui.pushButton->hide();
	}
}

PositionWid::~PositionWid()
{
}
void PositionWid::SetPosition(const Position& pos)
{
	ui.doubleSpinBox->setValue(pos.x);
	ui.doubleSpinBox_2->setValue(pos.y);
	ui.doubleSpinBox_3->setValue(pos.z);
	ui.doubleSpinBox_4->setValue(pos.a);
	ui.doubleSpinBox_5->setValue(pos.b);
	ui.doubleSpinBox_6->setValue(pos.c);

}
Position PositionWid::GetPosition()
{
	Position pos = {};

	pos.x = ui.doubleSpinBox->value();
	pos.y = ui.doubleSpinBox_2->value();
	pos.z = ui.doubleSpinBox_3->value();
	pos.a = ui.doubleSpinBox_4->value();
	pos.b = ui.doubleSpinBox_5->value();
	pos.c = ui.doubleSpinBox_6->value();

	return pos;
}

void PositionWid::on_pushButton_clicked()
{
	if (m_rob == nullptr) {
		m_rob = component->GetComponent(ComponentType::component_robot, true);
		if (m_rob == nullptr) {
			QMessageBox::warning(this, "提示", "获取机械手组件失败，请先配置机械手");
			return;
		}
	}
	QByteArray pos;
	if (m_rob->ExcuteCmd((int)RobotCmdType::robot_cmd_get_pos, {}, pos)) {
		PositionJ po = {};
		po.fromJson(pos);
		Position posi = {};
		posi.TransFromPositonJ(po);
		SetPosition(posi);
	} else {
		QMessageBox::warning(this, "提示", "机械手坐标获取失败,请重试");
	}
}
